<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 药品入库操作
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!--                                <el-input v-model="query.name" placeholder="用户名" class="handle-input mr10"></el-input>-->
                <!--                                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>-->
                <el-button type="primary" icon="el-icon-plus" @click="add" class="right">新增</el-button>
                <el-button type="primary" icon="el-icon-check" @click="submit" class="right">提交</el-button>
                <el-button type="primary" icon="el-icon-check" @click="test" class="right">test</el-button>
            </div>


            <el-table :data="addList" border class="table" ref="multipleTable" v-model="addList"
                      header-cell-class-name="table-header">
                <el-table-column type="index" label="ID" width="55" align="center"></el-table-column>
                <el-table-column label="门店" width="200" align="center">
                    <template #default="scope">
                        <el-select v-model="scope.row.shopId" placeholder="" class="handle-select mr10"
                                   style="width:180px">
                            <el-option v-for="item in shopList" :key="item.dictId" :label="item.dictValue"
                                       :value="item.dictId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>

                <el-table-column label="供应商名称" width="200" align="center">
                    <template #default="scope">
                        <el-select v-model="scope.row.appenderId" placeholder="" style="width:180px" class="handle-select mr10">
                            <el-option v-for="item in appenderList" :key="item.dictId" :label="item.dictValue"
                                       :value="item.dictId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="药品名称" width="210">
                    <template #default="scope">
                        <el-input v-model="scope.row.drugId">
                        </el-input>
                    </template>
                </el-table-column>

                <el-table-column label="规格" width="50" align="center">
                    <template #default="scope">
                        <el-select v-model="scope.row.specId" placeholder="" class="handle-select mr5" width="60">
                            <el-option v-for="item in specList" :key="item.dictId" :label="item.dictValue"
                                       :value="item.dictId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="数量" width="80">
                    <template #default="scope">
                        <el-input v-model="scope.row.drugCount">
                        </el-input>
                    </template>
                </el-table-column>

                <el-table-column label="单价" width="100">
                    <template #default="scope">
                        <el-input v-model="scope.row.drugPrice">
                            <template #prepend>￥</template>
                        </el-input>
                    </template>
                </el-table-column>

                <el-table-column label="有效期" width="160" align="center" padding>
                    <template #default="scope">
                        <el-date-picker
                                v-model="scope.row.validDate"
                                type="date"
                                placeholder="Select date and time"
                                style="width:135px;"
                        ></el-date-picker>
                    </template>

                </el-table-column>

                <el-table-column label="入库日期" prop="operateTime" width="160" align="center">

                </el-table-column>


                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-delete" class="red"
                                   @click.prevent="deleteRow(scope.$index, addList)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>


            <el-table :data="tableList" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column type="index" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="shopName" label="门店" width="200" align="center"></el-table-column>

                <el-table-column prop="appenderName" label="供应商名称" width="200" align="center"></el-table-column>
                <el-table-column prop="drugId"  label="药品名称" width="210"></el-table-column>

                <el-table-column prop="specName"  label="规格" width="50" align="center"></el-table-column>
                <el-table-column prop="drugCount"  label="数量" width="80"></el-table-column>

                <el-table-column prop="drugPrice"  label="单价" width="100"></el-table-column>

                <el-table-column   label="有效期" width="160" align="center">
                    <template #default="scope">
                        <el-date-picker
                                v-model="scope.row.validDate"
                                type="date"
                                placeholder="Select date and time"
                                :readonly=true
                                style="width:135px"

                        ></el-date-picker>
                    </template>
                </el-table-column>

                <el-table-column label="入库日期" prop="operateTime" width="160" align="center"></el-table-column>
            </el-table>
<!--            <div class="pagination">-->
<!--                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"-->
<!--                               :page-size="query.pageSize" :total="pageTotal"-->
<!--                               @current-change="handlePageChange"></el-pagination>-->
<!--            </div>-->
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="30%">
            <el-form label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
    import {ref, reactive} from "vue";
    import {ElMessage, ElMessageBox} from "element-plus";
    import {dateFormat} from "../utils/DateFormatteraUtil";
    import {addLog, queryTypeList,queryLogList} from "../api/index";


    export default {
        name: "tablein",
        inject: ['reload'],
        data() {
            return {
                addList: [],
            }
        },
        setup() {
            const query = reactive({
                address: "",
                name: "",
                pageIndex: 1,
                pageSize: 10,
            });
            const tableList = ref([]);
            const pageInfo = ref({});
            // 获取表格数据
            const getData = () => {
                queryLogList({operateType: 0}).then((res) => {
                    tableList.value = res.data.records
                });
            };
            const appenderList = ref([]);
            const shopList = ref([]);
            const specList = ref([]);
            queryTypeList('Appender').then((res) => {
                appenderList.value = res.data
            });
            queryTypeList('Shop').then((res) => {
                shopList.value = res.data
            });
            queryTypeList('Spec').then((res) => {
                specList.value = res.data
            });
            getData();

            // 查询操作
            const handleSearch = () => {
                query.pageIndex = 1;
                getData();
            };
            // 分页导航
            const handlePageChange = (val) => {
                query.pageIndex = val;
                getData();
            };

            // 删除操作
            const handleDelete = (index) => {
                // 二次确认删除
                ElMessageBox.confirm("确定要删除吗？", "提示", {
                    type: "warning",
                })
                    .then(() => {
                        ElMessage.success("删除成功");
                        tableData.value.splice(index, 1);
                    })
                    .catch(() => {
                    });
            };

            // 表格编辑时弹窗和保存
            const editVisible = ref(false);
            let form = reactive({
                name: "",
                address: "",
            });
            let idx = -1;
            const handleEdit = (index, row) => {
                idx = index;
                Object.keys(form).forEach((item) => {
                    form[item] = row[item];
                });
                editVisible.value = true;
            };
            const saveEdit = () => {
                editVisible.value = false;
                ElMessage.success(`修改第 ${idx + 1} 行成功`);
                Object.keys(form).forEach((item) => {
                    tableData.value[idx][item] = form[item];
                });
            };

            return {
                query,
                tableList,
                pageInfo,
                editVisible,
                form,
                handleSearch,
                handlePageChange,
                handleDelete,
                handleEdit,
                saveEdit,
                appenderList,
                shopList,
                specList,
            };
        },
        methods: {
            // 新增
            add: function () {
                this.addList.push({
                    drugID: null,
                    drugCount: null,
                    drugPrice: null,
                    operateTime: dateFormat("YYYY-mm-dd HH:MM:SS", new Date()).toLocaleString(),
                    operateType: "0"
                })
            },
            dictHandler: function (type, value) {

                if (type == 'operateType') {
                    return value === 'in'
                        ? '入库'
                        : value === 'out'
                            ? '出库'
                            : ''
                }
            },
            deleteRow(index, rows) {
                rows.splice(index, 1)
            },
            // 提交
            submit: function () {
                addLog(this.addList).then((res) => {
                    // 刷新页面
                    this.reload();
                });
            },
            // test
            test: function () {

                this.$store.commit("modalFlag",!this.$store.state.modalFlag);
                console.log(this.$store.state.modalFlag)
            }
        }
    }
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .mr5 {
        margin-right: 5px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
